﻿import flash.Lib;
import api.DAO;

/**
 * ...
 * @author Innokentij Shuvalov
 */

class Main 
{
	private static var apiId:		Int 	= 1850321;
	private static var fields:		String 	= "uid,first_name,last_name,sex,bdate,city,country,timezone";
	private static var apiSecret:	String 	= "676cGPAqlo";
	private static var viewerId:	Int		= 1;
	private static var apiURL:		String	= "http://api.vkontakte.ru/api.php";
	private static var Dao: DAO = new DAO(apiId, apiSecret, viewerId);
		
	
	private static function printData(user: UserInfo) {
		trace("Номер в контакте : " + user.uid);
		if (user.firstName != null) trace("Имя : " + user.firstName);
		if (user.lastName != null) trace("Фамилия : " + user.lastName);
		if (user.sex == 2) trace("Пол : мужской");
		if (user.sex == 1) trace("Пол : женский");
		if (user.birthDate != null) trace("Дата рождения : " + user.birthDate);
		
		var CountriesComplete = function (hash: IntHash <String> ) { trace("Страна : " + hash.get(user.country)); }
		Dao.getCountries([user.country], CountriesComplete, doNothing);
		
		var CitiesComplete = function (hash: IntHash <String> ) { trace("Город : " + hash.get(user.city)); }
		Dao.getCities([user.city], CitiesComplete, doNothing);
		
		trace("Временная зона : GMT+" + user.timezone);
	}
	
	private static function printNames (userInfos : Array < UserInfo >) {
		trace("");
		trace("Пять случайно выбранных друзей :");
		for (user in userInfos)
			trace(user.firstName + " " + user.lastName);
	}
	
	private static function print(user: UserInfo) {
		printData(user);
		
		var FriendsComplete = function (array: Array < Int > ) {
			var uids = [];
			for( i in 0...5 )
				uids[i] = array[ Std.int( Math.random() * array.length ) ];
		trace("Всего друзей : " + array.length);
		Dao.getProfiles(uids, null, ["uid,first_name,last_name"], printNames, doNothing);
		}
		var FriendsError = function (str: String) { trace(str);}
		Dao.getFriends(FriendsComplete, FriendsError);
	}
	
	private static function printAll (userInfos : Array < UserInfo >) {
		for (user in userInfos)
			print(user);
	}
	
	private static function reportError (error : String) {
		trace("Oops, error occured! \n" + error);
	}
	
	private static function doNothing (str: String) { }

	public static function main() 
	{
		viewerId = Std.parseInt( flash.Lib.current.loaderInfo.parameters.viewer_id );
		
		Dao = new DAO(apiId, apiSecret, viewerId);
				
		Dao.getProfiles([viewerId], null, [fields], printAll, reportError);
	}
	
}